<template> 
  <div>
    <a-upload :action="dataObj.host"
               :data="dataObj"
               list-type="picture"
               accept="image/*"
               :multiple="false"
               :show-file-list="showFileList"
               :default-file-list="fileList"
               :before-upload="beforeUpload"
               :on-remove="handleRemove"
               :on-success="handleUploadSuccess"
               :on-preview="handlePreview">
      <a-button size="small"
                 type="primary"
                 style="float: right;">上传图片</a-button>
      <!--<div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过10MB</div>-->
    </a-upload>
    <a-modal :visible.sync="dialogVisible">
      <img width="100%"
           :src="fileList[0].url">
    </a-modal>
  </div>
</template>
<script>
import { policy } from '@/utils/oss'

export default {
  name: 'singleUpload',
  props: {
    value: String,
    showList: {
      type: Boolean,
      default: true
    }
  },
  computed: {
    imageUrl() {
      return this.value;
    },
    imageName() {
      if (this.value != null && this.value !== '') {
        return this.value.substr(this.value.lastIndexOf("/") + 1);
      } else {
        return null;
      }
    },
    fileList() {
      return [{
        name: this.imageName,
        url: this.imageUrl
      }]
    },
    showFileList: {
      get: function () {
        return this.value !== null && this.value !== '' && this.value !== undefined && this.showList;
      },
      set: function (newValue) {
      }
    }
  },
  data() {
    return {
      dataObj: {
        policy: '',
        signature: '',
        key: '',
        ossaccessKeyId: '',
        dir: '',
        host: 'https://oss-cn-beijing.aliyuncs.com'
      },
      dialogVisible: false
    };
  },
  methods: {
    emitInput(val) {
      this.$emit('input', val)
    },
    handleRemove(file, fileList) {
      this.emitInput('')
    },
    handlePreview(file) {
      this.dialogVisible = true
    },
    beforeUpload(file) {
      let _self = this
      // 最大只能上传10M文件
      if (file.size > 10485760) {
        this.$message.error('上传失败，最大只能上传10M大小的图片')
        return false
      }
      return new Promise((resolve, reject) => {
        policy().then(response => {
          _self.dataObj.policy = response.result.policy
          _self.dataObj.signature = response.result.signature
          _self.dataObj.ossaccessKeyId = response.result.accessKeyId
          _self.dataObj.key = response.result.dir + '/' + file.uid + file.name.replace(/,/g, '')
          _self.dataObj.dir = response.result.dir
          _self.dataObj.host = response.result.host
          resolve(true)
        }).catch(err => {
          console.log(err)
          reject(false)
        })
      })
    },
    handleUploadSuccess(res, file) {
      this.showFileList = this.showList
      this.fileList.pop()
      this.fileList.push({
        name: file.uid + file.name,
        url: this.dataObj.host + '/' + this.dataObj.dir + '/' + file.uid + file.name.replace(/,/g, '')
      })
      this.emitInput(this.fileList[0].url)
    }
  }
}
</script>
<style scoped>
</style>